<template>
  <div class="my-tours">
    <div class="page-header">
      <h2>我的旅游团</h2>
      <el-button type="primary" @click="handleCreate">创建新团</el-button>
    </div>

    <el-table v-loading="loading" :data="tours" style="width: 100%">
      <el-table-column prop="name" label="团名" min-width="200" />
      <el-table-column prop="city" label="城市" width="120" />
      <el-table-column prop="price" label="价格" width="120">
        <template #default="{ row }">
          ¥{{ row.price }}
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.status === 1 ? 'success' : 'info'">
            {{ row.status === 1 ? '进行中' : '已结束' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button-group>
            <el-button size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <!-- 创建/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="editingTour ? '编辑旅游团' : '创建旅游团'"
      width="50%"
    >
      <el-form
        ref="formRef"
        :model="tourForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="团名" prop="name">
          <el-input v-model="tourForm.name" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="tourForm.description" type="textarea" :rows="2" />
        </el-form-item>
        <el-form-item label="封面图" prop="imageUrl">
          <el-input v-model="tourForm.imageUrl" placeholder="请输入图片URL" />
        </el-form-item>
        <el-form-item label="城市" prop="city">
          <el-input v-model="tourForm.city" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="tourForm.address" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactInfo">
          <el-input v-model="tourForm.contactInfo" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input-number v-model="tourForm.price" :precision="2" :step="10" />
        </el-form-item>
        <el-form-item label="详细信息" prop="details">
          <el-input
            v-model="tourForm.details"
            type="textarea"
            :rows="4"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm" :loading="submitting">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { userServiceApi } from '@/api/user/service'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance } from 'element-plus'
import type { Service } from '@/types/service'

const loading = ref(false)
const submitting = ref(false)
const tours = ref<Service[]>([])
const dialogVisible = ref(false)
const editingTour = ref<Service | null>(null)
const formRef = ref<FormInstance>()

const tourForm = ref({
  name: '',
  description: '',
  type: 'TOUR',
  price: 0,
  city: '',
  address: '',
  contactInfo: '',
  details: '',
  imageUrl: ''
})

const rules = {
  name: [{ required: true, message: '请输入团名', trigger: 'blur' }],
  description: [{ required: true, message: '请输入描述', trigger: 'blur' }],
  imageUrl: [{ required: true, message: '请输入封面图URL', trigger: 'blur' }],
  city: [{ required: true, message: '请输入城市', trigger: 'blur' }],
  address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
  contactInfo: [{ required: true, message: '请输入联系方式', trigger: 'blur' }],
  price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
  details: [{ required: true, message: '请输入详细信息', trigger: 'blur' }]
}

const fetchTours = async () => {
  loading.value = true
  try {
    const response = await userServiceApi.getMyServices()
    if (response.data.code === 200) {
      tours.value = response.data.data
    }
  } catch (error) {
    console.error('获取旅游团列表失败:', error)
    ElMessage.error('获取旅游团列表失败')
  } finally {
    loading.value = false
  }
}

const handleCreate = () => {
  editingTour.value = null
  tourForm.value = {
    name: '',
    description: '',
    type: 'TOUR',
    price: 0,
    city: '',
    address: '',
    contactInfo: '',
    details: '',
    imageUrl: ''
  }
  dialogVisible.value = true
}

const handleEdit = (row: Service) => {
  editingTour.value = row
  tourForm.value = { ...row }
  dialogVisible.value = true
}

const handleDelete = (row: Service) => {
  ElMessageBox.confirm('确定要删除这个旅游团吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    try {
      const response = await userServiceApi.delete(row.id)
      if (response.data.code === 200) {
        ElMessage.success('删除成功')
        fetchTours()
      }
    } catch (error) {
      console.error('删除旅游团失败:', error)
      ElMessage.error('删除旅游团失败')
    }
  })
}

const submitForm = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate(async (valid) => {
    if (valid) {
      submitting.value = true
      try {
        const response = editingTour.value
          ? await userServiceApi.update(editingTour.value.id, tourForm.value)
          : await userServiceApi.create(tourForm.value)
        
        if (response.data.code === 200) {
          ElMessage.success(editingTour.value ? '更新成功' : '创建成功')
          dialogVisible.value = false
          fetchTours()
        }
      } catch (error) {
        console.error(editingTour.value ? '更新旅游团失败:' : '创建旅游团失败:', error)
        ElMessage.error(editingTour.value ? '更新旅游团失败' : '创建旅游团失败')
      } finally {
        submitting.value = false
      }
    }
  })
}

onMounted(() => {
  fetchTours()
})
</script>

<style scoped>
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

:deep(.el-dialog__body) {
  padding-top: 20px;
}
</style> 